package app_demo.design;

/**
 * @Description 单例模式
 * @Author Firenut
 * @Date 2023-02-16 14:50
 */
public class Singleton {
    // volatile保证可见性、static修饰保证只有一份
    private volatile static Singleton singleton;

    // 私有构造器,防止直接通过new的方式创建对象
    private Singleton() {
    }

    // 获取单例对象
    public Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                    return singleton;
                }
            }
        }
        return singleton;
    }
}